package ${configure.packageName}.${generatorTable.moudleName}.controller.${generatorTable.fileDirName};

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cyys.common.annotation.SystemLog;
import com.cyys.common.ql.AjaxJson;
import com.cyys.common.ql.JSONData;
import com.cyys.common.utils.StringUtil;
import com.cyys.modules.core.controller.BaseAction;
import com.cyys.modules.core.sysParam.ParamBasicUtil;
import com.cyys.modules.core.sysParam.ParamConstants;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.cyys.common.validate.Detail;
import com.cyys.common.validate.Edit;
import com.cyys.common.validate.Insert;

import javax.annotation.Resource;
import java.util.List;

import ${configure.packageName}.${generatorTable.moudleName}.service.${generatorTable.fileDirName}.${generatorTable.upperClassName}Service;
import ${configure.packageName}.${generatorTable.moudleName}.model.${generatorTable.fileDirName}.${generatorTable.upperClassName};


@Controller
@RequestMapping("/${generatorTable.moudleName}/${generatorTable.shiro}")
public class ${generatorTable.upperClassName}Action extends BaseAction {

	@Resource
	private ${generatorTable.upperClassName}Service ${generatorTable.className}Service;

	@GetMapping("/add/input")
	@RequiresPermissions("${generatorTable.moudleName}:${generatorTable.shiro}:add")
	public String addInput(Model model) throws Exception {
	<#if generatorTable.paramNameList?exists && generatorTable.paramNameList?size gt 0>
		<#list generatorTable.paramNameList as fieldItem >
		model.addAttribute("${fieldItem}_MAP", ParamBasicUtil.getParamBasicTypeMap().get(ParamConstants.${fieldItem}));
		</#list>
	</#if>
		return "module/${generatorTable.moudleName}/${generatorTable.fileDirName}/${generatorTable.shiro}/${generatorTable.className}-input";
	}

	@ResponseBody
	@PostMapping("/add/save")
	@RequiresPermissions("${generatorTable.moudleName}:${generatorTable.shiro}:add")
	@SystemLog(module = "${generatorTable.tableComment!""}", methods = "${generatorTable.tableComment!""}管理-保存${generatorTable.tableComment!""}")
	public AjaxJson save(@Validated(Insert.class) ${generatorTable.upperClassName} ${generatorTable.className}) throws Exception {
		return ${generatorTable.className}Service.save(${generatorTable.className});
	}

	@GetMapping("/edit/input")
	@RequiresPermissions("${generatorTable.moudleName}:${generatorTable.shiro}:edit")
	public String updateInput(@Validated(Detail.class) ${generatorTable.upperClassName} ${generatorTable.className}, Model model) throws Exception {
	<#if generatorTable.paramNameList?exists && generatorTable.paramNameList?size gt 0>
		<#list generatorTable.paramNameList as fieldItem >
		model.addAttribute("${fieldItem}_MAP", ParamBasicUtil.getParamBasicTypeMap().get(ParamConstants.${fieldItem}));
		</#list>
	</#if>
		model.addAttribute("${generatorTable.className}", ${generatorTable.className}Service.getById(${generatorTable.className}.getId()));
		return "module/${generatorTable.moudleName}/${generatorTable.fileDirName}/${generatorTable.shiro}/${generatorTable.className}-input";
	}

	@ResponseBody
	@PostMapping("/edit/save")
	@RequiresPermissions("${generatorTable.moudleName}:${generatorTable.shiro}:edit")
	@SystemLog(module = "${generatorTable.tableComment!""}", methods = "${generatorTable.tableComment!""}管理-保存${generatorTable.tableComment!""}")
	public AjaxJson edit(@Validated(Edit.class) ${generatorTable.upperClassName} ${generatorTable.className}) throws Exception {
		return ${generatorTable.className}Service.save(${generatorTable.className});
	}

	@ResponseBody
	@PostMapping("/del/deleteById")
	@RequiresPermissions("${generatorTable.moudleName}:${generatorTable.shiro}:del")
	@SystemLog(module = "${generatorTable.tableComment!""}", methods = "${generatorTable.tableComment!""}管理-删除${generatorTable.tableComment!""}")
	public AjaxJson deleteById(String id) throws Exception{
		return ${generatorTable.className}Service.deleteById(id);
	}

	@ResponseBody
	@PostMapping("/del/deleteByIds")
	@RequiresPermissions("${generatorTable.moudleName}:${generatorTable.shiro}:del")
	@SystemLog(module = "${generatorTable.tableComment!""}", methods = "${generatorTable.tableComment!""}管理-批量删除${generatorTable.tableComment!""}")
	public AjaxJson deleteByIds(String ids) throws Exception {
		return ${generatorTable.className}Service.deleteBatchIds(ids);
	}

	@GetMapping("/list")
	@RequiresPermissions("${generatorTable.moudleName}:${generatorTable.shiro}:menu")
	public String list(Model model) {
	<#if generatorTable.paramNameList?exists && generatorTable.paramNameList?size gt 0>
		<#list generatorTable.paramNameList as fieldItem >
		model.addAttribute("${fieldItem}_MAP", ParamBasicUtil.getParamBasicTypeMap().get(ParamConstants.${fieldItem}));
		</#list>
	</#if>
		return "module/${generatorTable.moudleName}/${generatorTable.fileDirName}/${generatorTable.shiro}/${generatorTable.className}-list";
	}


	@ResponseBody
	@PostMapping("/list/listInfo")
	@RequiresPermissions("${generatorTable.moudleName}:${generatorTable.shiro}:list")
	public String listInfo(${generatorTable.upperClassName} ${generatorTable.className}, Page page) throws Exception {
		QueryWrapper<${generatorTable.upperClassName}> qw = new QueryWrapper<>();
		qw.setEntity(${generatorTable.className});
		qw.orderByAsc("parent_id").orderByAsc("sort");
		return JSONData.toLayerData(${generatorTable.className}Service.list(page , qw ,false));
	}

	/**

	 * @param parentId 父节点编号
	 * @param queryTop 是否查询顶级节点
	 * @param isFirst 是否只查第一级节点
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@PostMapping("/list/getByParentId")
	public String getByParentId(String parentId, Boolean queryTop,Boolean isFirst) throws Exception {
		if (queryTop == null) { // 是否查询顶级节点
			queryTop = true;
		}
		if (isFirst == null) {
			isFirst = true;
		}
		List<${generatorTable.upperClassName}> list = ${generatorTable.className}Service.listByParentId(parentId, isFirst);
		String array = "[";
		if (queryTop) {
			array += "{id:\"0\"," + "pId:\"\",name:\"顶级节点\"";
			array += ",isParent:false"; //这里为true的话，前台顶级节点一直点一直有
			array += ",dropRoot:false";
			array += ",childOuter:false";
			array += ",open:true},";
		}
		array += getArray(list);
		int indx = array.lastIndexOf(",");
		if (indx != -1)
			array = array.substring(0, indx);
		array += "]";
		return array;
	}

	private String getArray(List<${generatorTable.upperClassName}> list) {
		String array = "";
		for (int i = 0; i < list.size(); i++) {
			${generatorTable.upperClassName} t = list.get(i);
			array += "{id:\"" + t.getId() + "\"," + "pId:\"" + t.getParentId() + "\",name:\"" + t.getName() + "\"";
			if (t.getHasChild() == 1) {
//				array += ",isParent:true";
				array += ",dropRoot:false";
				array += ",childOuter:false";
			} else {
				array += ",dropInner:false,childOuter:false";
			}
			if (t.getChildList() != null && t.getChildList().size() > 0) {
				array += ",open:true},";
				array += getArray(t.getChildList());
			} else {
				array += "},";
			}
		}
		return array;
	}

	@GetMapping("select")
	public String select(Model model, Boolean queryTop,Boolean isFirst) {
		model.addAttribute("queryTop", queryTop);
		model.addAttribute("isFirst", isFirst);
		return "module/${generatorTable.moudleName}/${generatorTable.fileDirName}/${generatorTable.shiro}/${generatorTable.className}-select";
	}

}
